package edu.bit.water.service;

import edu.bit.water.dao.WarningRecordDao;
import edu.bit.water.dao.WarningRuleDao;
import edu.bit.water.dto.WaterWarningCondition;
import edu.bit.water.dto.WarningRule;
import edu.bit.water.entity.WarningRecord;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.bit.water.dto.DateUtils;


/**
 * @Description: WarningRecord的service层
 * @Author: YYF
 */
@Service
public class WarningRecordService {
    @Autowired
    private WarningRecordDao warningRecordDao;

    public List<String> selectAllSensors() {
        return warningRecordDao.selectAllSensors();
    }

    public List<Map<String, Object>> queryWarningRecord(String sensorRowId, WaterWarningCondition waterWarningCondition) {
        return warningRecordDao.queryWarningRecord(sensorRowId, waterWarningCondition);
    }

    //根据传感器编号和查询日期条件筛选出数据
    public List<HashMap<String, Object>> queryRecordByCondition(String sensorRowId, WaterWarningCondition waterWarningCondition) {
        return warningRecordDao.queryRecordByCondition(sensorRowId, waterWarningCondition);
    }

    public List<String> querySensorNames(String sensorRowId, Integer companyId){
        return warningRecordDao.querySensorNames(sensorRowId, companyId);
    }

    public List<String> queryWorkshop() {
        return warningRecordDao.queryWorkshop();
    }

    public List<String> queryWorkshopByCompanyId(int company_id) {

        return warningRecordDao.queryWorkshopByCompanyId(company_id);
    }

    public List<String> querySortByWorkshop(String workShopSql, String techProcessId){
        return warningRecordDao.querySortByWorkshop(workShopSql, techProcessId);
    }

    public List<String> querySensorRowIdByWorshopAndsort(String techProcessId,  String workShopSql, String sortSql){
        return warningRecordDao.querySensorRowIdByWorshopAndsort(techProcessId, workShopSql,sortSql);
    };

    public List<String> querySensorRowIdByWorkshopAndsort(String businessId,  String workShopSql, String sortSql){
        return warningRecordDao.querySensorRowIdByWorkshopAndsort(businessId, workShopSql,sortSql);
    };

    public List<WarningRule> queryWarningRuleBySensorId(String sensorID) {
        return warningRecordDao.queryWarningRuleBySensorId(sensorID);
    }

    public List<WarningRule> queryWarningRuleBySensorIdAndCompanyId(String sensorID, int company_id) {
        return warningRecordDao.queryWarningRuleBySensorIdAndCompanyId(sensorID, company_id);
    }
	
	/*
    查询各个时间段内预警信息
     */
    public Map<String, Object> queryIndexData() {
        java.text.DecimalFormat df =new java.text.DecimalFormat("#.00");
        Map<String, Object> result = new HashMap<>();
        double radis = getWarningRecordData(DateUtils.getBeforeNowOneHourDate(), DateUtils.getNowDate(), warningRecordDao);
        String res = df.format(radis * 100);
        result.put("hour", res);
        radis = getWarningRecordData(DateUtils.getBeforeNowOneDayDate(), DateUtils.getNowDate(), warningRecordDao);
        res =  df.format(radis * 100);
        result.put("day", res);
//        radis = getWarningRecordData(DateUtils.getBeforeNowOneWeekDate(), DateUtils.getNowDate(), warningRecordDao);
//        res = df.format(radis * 100);
//        result.put("week", res);
//        radis = getWarningRecordData(DateUtils.getBeforeNowOneMonthDate(), DateUtils.getNowDate(), warningRecordDao);
//        res = df.format(radis * 100);
//        result.put("month", res);
        return result;
    }


    private static double getWarningRecordData(String startTime, String endTime, WarningRecordDao warningRecordDao){
        //获取全部预警规则
        List<WarningRule> rules = warningRecordDao.getAllRules();
        //获取时间段内水信息总数
        int sum_entity_water = warningRecordDao.getSumOfEntityWater(startTime, endTime);
        if (sum_entity_water == 0) {
            return 0.0;
        }
        int sum = 0;
        for (int i = 0; i < rules.size(); i++) {
            int temp = warningRecordDao.getSumOfWarningRecord(startTime, endTime, rules.get(i));
            sum = sum + temp;
        }
        return (double)sum / sum_entity_water;
    }

    public Date queryLastTime() {
        return warningRecordDao.queryLastTime();
    }

    public int addWarningRecord(WarningRecord warningRecord) {
        return warningRecordDao.addWarningRecord(warningRecord);
    }

    public List<WarningRecord> queryWarningRecordByWarningConditionAndCompanyId(WaterWarningCondition condition){
        return warningRecordDao.queryWarningRecordByWarningConditionAndCompanyId(condition);
    }
}
